package com.cgbl.cg.vm.resource

import android.app.Application
import androidx.databinding.ObservableField
import api.ResourceServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.model.PageListBean
import com.robot.base.model.ResourceBean
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse

class ExcellentResourceViewModel(application: Application) : GLBaseViewModel(application) {

    val type = ObservableField<Int>(0)
    val filterName = ObservableField("全部")
    var filterType = 0
    val list = arrayListOf<String>()
    val tag = ObservableField(true)

    val uc = UiChangeEvent()
    var pageNo = 1

    class UiChangeEvent {
        val showFilter: SingleLiveEvent<ArrayList<String>> = SingleLiveEvent()
        val refreshEvent = SingleLiveEvent<Int>()
        val refreshDataEvent = SingleLiveEvent<List<ResourceBean>>()
    }

    val leftTagCommand = BindingCommand<Any>(BindingAction {
        tag.set(true)
        refresh()
    })

    val rightTagCommand = BindingCommand<Any>(BindingAction {
        tag.set(false)
        refresh()
    })

    val filterCommand = BindingCommand<Any>(BindingAction {
        uc.showFilter.call()
    })

    fun initData() {
        list.addAll(arrayListOf("全部", "免费专区", "付费专区", "VIP限免"))
        refresh()
    }

    fun refresh() {
        pageNo = 1
        page()
    }

    fun load() {
        pageNo++
        page()
    }

    fun page() {
        val map = hashMapOf<String, Any>(
            "pageNo" to pageNo,
            "pageSize" to 10,
            "isAccount" to 0,
            "sort" to if (tag.get() == true) {
                0
            } else {
                1
            }
        )
        when (filterType) {
            1 -> {
                map["isFree"] = 1
            }
            2 -> {
                map["isFree"] = 0
            }
            3 -> {
                map["isFree"] = 2
            }
        }
        when (type.get()) {
            2 -> {
                map["naturalRecommend"] = 2
            }

            1 -> {
                map["naturalRecommend"] = 1
            }

            0->{
                map["naturalRecommend"] = 3
            }
        }
        ResourceServiceFactory.page(
            map
        ).subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<PageListBean<ResourceBean>>>(this) {
            override fun onNext(t: BaseResponse<PageListBean<ResourceBean>>) {
                uc.refreshDataEvent.postValue(t.data.list)
                uc.refreshEvent.postValue(
                    if (t.data.total.toInt() < pageNo * 10) {
                        0
                    } else {
                        1
                    }
                )
            }

            override fun onError(e: Throwable) {
                super.onError(e)
                uc.refreshEvent.postValue(
                    0
                )
            }

        })
    }
}